package com.jianyushe.jianyushebaseadmin.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @author 11584
 * @author Create by Alvin
 * @Description swagger配置类
 * 学习 https://www.bilibili.com/video/BV1Y441197Lw?p=3&spm_id_from=333.880.my_history.page.click
 * @date 2022-04-10 15:59
 */
@Configuration
@EnableSwagger2 // 开启Swagger2的自动配置
public class SwaggerConfig {
    @Autowired
    private Environment environment;

    @Bean //配置docket以配置Swagger具体参数
    public Docket docket() {
        // 设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev", "test");
        // 通过Environment.acceptsProfiles判断是否处在自己设定的环境当中
        boolean flag = environment.acceptsProfiles(profiles);
        System.out.println("flag--->" + flag);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                // 配置石佛开启swagger，如果为false，则swagger不能在浏览器中访问
                .enable(flag)
                // 配置过滤
                .select()
                //.apis(RequestHandlerSelectors.basePackage("com.jianyushe.jianyushebaseadmin.controller"))
                // 过滤路径
                //.paths(PathSelectors.ant("/admin/**"))
                .build();
    }

    private ApiInfo apiInfo() {
        Contact DEFAULT_CONTACT = new Contact("Alvin", "http://www.github.com", "globalsvip@gmail.com");
        return new ApiInfo(
                "JianYushebase Documentation",
                "JianYushebase Documentation",
                "1.0",
                "http://www.JianYushebase.com/",
                DEFAULT_CONTACT,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<VendorExtension>());

    }
}
